{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "documentary-joint", "metadata": {}, "outputs": [], "source": [ "# This tutorial will introduce simple implementations of conditional if\n", "# statements in Python." ] }, { "cell_type": "code", "execution_count": null, "id": "muslim-imaging", "metadata": {}, "outputs": [], "source": [ "# Here's a simple if statement. If statements use the same indentation\n", "# scheme as for loops. Indented lines immediately following the if \n", "# statement are within the if control sturcture. The if statement ends\n", "# when the indented block of code ends.\n", "ans = 'y'\n", "if ans == 'y':\n", " print('yes')" ] }, { "cell_type": "code", "execution_count": null, "id": "entitled-vegetable", "metadata": {}, "outputs": [], "source": [ "# In the example above, ans was set equal to 'y' and the if condition was\n", "# true such that 'yes' was printed. In the example below, ans is not equal\n", "# to 'y' and the if condition is false. As a result, no output is printed.\n", "ans = 'n'\n", "if ans == 'y':\n", " print('yes')" ] }, { "cell_type": "code", "execution_count": null, "id": "floral-channels", "metadata": {}, "outputs": [], "source": [ "# We can enhance the capability of our if statement by adding an else\n", "# statement. Anytime the if condition is false, the else statement will\n", "# execute.\n", "\n", "# True, so 'yes' output.\n", "ans = 'y'\n", "if ans == 'y':\n", " print('yes')\n", "else:\n", " print('no')" ] }, { "cell_type": "code", "execution_count": null, "id": "raising-puzzle", "metadata": {}, "outputs": [], "source": [ "# False, so 'no' output.\n", "ans = 'n'\n", "if ans == 'y':\n", " print('yes')\n", "else:\n", " print('no')" ] }, { "cell_type": "code", "execution_count": null, "id": "figured-tribe", "metadata": {}, "outputs": [], "source": [ "# False, so 'no' output.\n", "ans = 'x'\n", "if ans == 'y':\n", " print('yes')\n", "else:\n", " print('no')" ] }, { "cell_type": "code", "execution_count": null, "id": "happy-circumstances", "metadata": {}, "outputs": [], "source": [ "# In the above if statement the only \"y\" produces a \"yes\", any other entry\n", "# results in \"no\". Suppose that you only want to accept \"y\" and \"n\" as\n", "# valid responses. Including an 'else if' condtion (using 'elif') as done \n", "# below produces the desired results." ] }, { "cell_type": "code", "execution_count": null, "id": "endangered-worth", "metadata": {}, "outputs": [], "source": [ "# If condition true, so output 'yes'.\n", "ans = 'y'\n", "if ans == 'y':\n", " print('yes')\n", "elif ans == 'n':\n", " print('no')\n", "else:\n", " print('invalid response')" ] }, { "cell_type": "code", "execution_count": null, "id": "dietary-cornwall", "metadata": {}, "outputs": [], "source": [ "# Elseif condition true, so output 'no'.\n", "ans = 'n';\n", "if ans == 'y':\n", " print('yes')\n", "elif ans == 'n':\n", " print('no')\n", "else:\n", " print('invalid response')" ] }, { "cell_type": "code", "execution_count": null, "id": "capable-blind", "metadata": {}, "outputs": [], "source": [ "# If and elseif conditions false, so output 'invalid response'.\n", "ans = 'x';\n", "if ans == 'y':\n", " print('yes')\n", "elif ans == 'n':\n", " print('no')\n", "else:\n", " print('invalid response')" ] }, { "cell_type": "code", "execution_count": 12, "id": "modified-vitamin", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your value: physics\n", "invalid response\n" ] } ], "source": [ "# To complete this tutorial, here's how you can prompt the user for an\n", "# input in Python. All you need to do is use the 'input()' statement.\n", "val = input(\"Enter your value: \") \n", "if val == 'y':\n", " print('yes')\n", "elif val == 'n':\n", " print('no')\n", "else:\n", " print('invalid response')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }